package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BkArticleMapper;
import com.ruoyi.system.domain.BkArticle;
import com.ruoyi.system.service.IBkArticleService;

/**
 * 文章信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-12
 */
@Service
public class BkArticleServiceImpl implements IBkArticleService 
{
    @Autowired
    private BkArticleMapper bkArticleMapper;
//    @Autowired
//    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 查询文章信息
     * 
     * @param id 文章信息主键
     * @return 文章信息
     */
    @Override
    public BkArticle selectBkArticleById(Long id)
    {
        return bkArticleMapper.selectBkArticleById(id);
    }

    /**
     * 查询文章信息列表
     * 
     * @param bkArticle 文章信息
     * @return 文章信息
     */
    @Override
    public List<BkArticle> selectBkArticleList(BkArticle bkArticle)
    {
        return bkArticleMapper.selectBkArticleList(bkArticle);
    }

    /**
     * 新增文章信息
     * 
     * @param bkArticle 文章信息
     * @return 结果
     */
    @Override
    public int insertBkArticle(BkArticle bkArticle)
    {
        bkArticle.setCreateTime(DateUtils.getNowDate());
        return bkArticleMapper.insertBkArticle(bkArticle);
    }

    /**
     * 修改文章信息
     * 
     * @param bkArticle 文章信息
     * @return 结果
     */
    @Override
    public int updateBkArticle(BkArticle bkArticle)
    {
        bkArticle.setUpdateTime(DateUtils.getNowDate());
        return bkArticleMapper.updateBkArticle(bkArticle);
    }

    /**
     * 批量删除文章信息
     * 
     * @param ids 需要删除的文章信息主键
     * @return 结果
     */
    @Override
    public int deleteBkArticleByIds(Long[] ids)
    {
        return bkArticleMapper.deleteBkArticleByIds(ids);
    }

    /**
     * 删除文章信息信息
     * 
     * @param id 文章信息主键
     * @return 结果
     */
    @Override
    public int deleteBkArticleById(Long id)
    {
        return bkArticleMapper.deleteBkArticleById(id);
    }

    // 每2分钟同步一次（可配置）
    @Scheduled(cron = "0 */15 * * * ?")
    public void syncViewCount() {
        System.out.println(">>>> 已执行1");
        HashOperations<Object, Object, Object> hashOps = redisTemplate.opsForHash();
        Map<Object, Object> viewMap = hashOps.entries("article:views");
        System.out.println(">>>> 已执行2" + viewMap);
        List<BkArticle> updateList = new ArrayList<>();
        viewMap.forEach((articleId, views) -> {
            BkArticle article = new BkArticle();
            article.setId(Long.parseLong(articleId.toString()));
            article.setViewCount(Long.parseLong(views.toString()));
            updateList.add(article);
        });

        if(!updateList.isEmpty()) {
            System.out.println(">>>> 已执行3" + updateList);
            // 批量更新数据库
            bkArticleMapper.batchUpdateViewCount(updateList);
            // 清空Redis缓存（根据业务需求选择是否清空）
            redisTemplate.delete("article:views");

        }
    }
}
